home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14876 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.4 KB

  1. Path: news.iastate.edu!news
  2. From: Marybeth Gurski <gurski@cs.iastate.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Template constructor problem
  5. Date: Tue, 02 Apr 1996 08:48:18 -0600
  6. Organization: Dept. Of Computer Science, Iowa State University
  7. Message-ID: <31613E32.1D07@cs.iastate.edu>
  8. References: <4jjtgf$ebo@csugrad.cs.vt.edu>
  9. NNTP-Posting-Host: sunfire.cs.iastate.edu
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.07 9000/712)
  14.  
  15. Raj Suri wrote:
  16. > Ok, take this code using g++ v2.7
  17. > queue.h:
  18. > template <class QueueItem> class Queue {
  19. >  private:
  20. >   QueueItem buffer[100];
  21. >   int head, tail, count;
  22. >  public:
  23. >   Queue();
  24. >   void Insert (QueueItem item);
  25. >   QueueItem Remove();
  26. >   int Size();
  27. >   ~Queue();
  28. > };
  29. > template <class QueueItem>
  30. > Queue<QueueItem>::Queue() : count(0), head(0), tail(0) {}
  31. > .....[the rest of the mothods]
  32. > main.cc:
  33. > #include<stdio.h>
  34. > #include "queue.h"
  35. > Queue<int> myqueue;
  36. > main () {
  37. > }
  38. > Compiler error when linking:
  39. > collect2: ld returned 1 exit status
  40. > /usr/lib/cmplrs/cc/ld:
  41. > Undefined:
  42. > Queue<int>::Queue(void)
  43. > If I don't declare a queue object, then I don't get the linker error.
  44. > Any suggestion??  Thanks in advance.
  45.  
  46. I've had similar problems with this version of g++.  It does't 
  47. support templates the way that you want it to.  From the FAQ for g++:
  48.  
  49.  
  50. -----
  51.    * Support for automatic template instantiation has *not* been enabled
  52.      in the official distribution, due to a disagreement over design
  53.      philosophies.  But you can get a patch from Cygnus to turn it on;
  54.      retrieve the patch from
  55.      `ftp://ftp.cygnus.com/pub/g++/gcc-2.7.0-repo.gz'.
  56. ------
  57. and:
  58. ------
  59. How do I use the new repository code?
  60. =====================================
  61.  
  62.    Because there is some disagreement about the details of the template
  63. repository mechanism, you'll need to obtain a patch from Cygnus Support
  64. to enable the 2.7.0 repository code.  You can obtain the patch by
  65. anonymous FTP: `ftp://ftp.cygnus.com/pub/g++/gcc-2.7.0-repo.gz'.
  66.  
  67.    After you've applied the patch, the `-frepo' flag will enable the
  68. repository mechanism.  The flag works much like the existing
  69. `-fno-implicit-templates' flag, except that auxiliary files, with an
  70. `.rpo' extension, are built that specify what template expansions are
  71. needed.  At link time, the (patched) collect program detects missing
  72. templates and recompiles some of the object files so that the required
  73. templates are expanded.
  74.  
  75.    Note that the mechanism differs from that of cfront in that template
  76. definitions still must be visible at the point where they are to be
  77. expanded.  No assumption is made that `foo.C' contains template
  78. definitions corresponding to template declarations in `foo.h'.
  79.  
  80.    Jason Merrill writes: "To perform closure on a set of objects, just
  81. try to link them together.  It will fail, but as a side effect all
  82. needed instances will be generated in the objects."
  83.  
  84. -------
  85. Problems with the template implementation
  86. =========================================
  87.  
  88.    g++ does not implement a separate pass to instantiate template
  89. functions and classes at this point; for this reason, it will not work,
  90. for the most part, to declare your template functions in one file and
  91. define them in another.  The compiler will need to see the entire
  92. definition of the function, and will generate a static copy of the
  93. function in each file in which it is used.
  94.  
  95.    (The experimental template repository code (see *Note repository::)
  96. that can be added to 2.7.0 does implement a separate pass, but there is
  97. still no searching of files that the compiler never saw).
  98.  
  99.    For version 2.6.0, however, a new switch `-fno-implicit-templates'
  100. was added; with this switch, templates are expanded only under user
  101. control.  I recommend that all g++ users that use templates read the
  102. section "Template Instantiation" in the gcc manual (version 2.6.x and
  103. newer).  g++ now supports explicit template expansion using the syntax
  104. from the latest C++ working paper:
  105.  
  106.      template class A<int>;
  107.      template ostream& operator << (ostream&, const A<int>&);
  108. ---------------
  109.  
  110. So what you might have to do is put
  111.     template class Queue<int>;
  112. in your Queue.C file.
  113.  
  114.  
  115. There is lots of more information in the FAQ for g++.
  116.  
  117.  
  118. -- Marybeth
  119.  
  120.  
  121. -- 
  122.     --------------------------------------------------------------
  123.                Marybeth Gurski
  124.             gurski@cs.iastate.edu
  125.        http://www.cs.iastate.edu/~gurski/homepage.html
  126.